﻿#include "DeviceSettingsTab.h"
#include "ui_DeviceSettingsTab.h"
#include "global.h"
#include "Sqlite/DeviceDBManager.h"
#include "MotorManager/KincoServoManager.h"
#include <QPainter>
#include <QJsonObject>
#include <QMessageBox>
#include <QTableWidget>
#include <QAbstractItemView>
#include <QStandardItemModel>

DeviceSettingsTab::DeviceSettingsTab(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DeviceSettingsTab)
{
    ui->setupUi(this);
    this->setWindowState(Qt::WindowState::WindowMaximized);
    //主窗口关闭，一同关闭子窗口
    setAttribute(Qt::WA_QuitOnClose,false);

    //tab1初始化
    //默认不可编辑
    ui->tb_sub->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tb_port->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //drawGrid();
    //从数据库中加载节和层设置表格
    loadTableSub();
    //从数据库中加载出口设置表格
    loadTablePort();
    //从数据库中加载设置信息
    loadDeviceInfo();

    //tab2初始化
    ui->fm_useradd->setVisible(false);
    ui->fm_pwdalter->setVisible(false);
    loadTableUser();
}

DeviceSettingsTab::~DeviceSettingsTab()
{
    delete ui;
}

void DeviceSettingsTab::drawGrid()
{
    QPixmap pixmap(ui->lb_image->size());
    pixmap.fill(Qt::transparent);

    QPainter painter(&pixmap);
    painter.setPen(Qt::black);

    int sub = 3;
    int layer = 4;

    QList<int> x_position;
    QList<int> y_position;

    int w = ui->lb_image->width()-300;   // 窗口宽度
    int h = ui->lb_image->height()-50;  // 高度,上下各留50
    int step_x = w/sub;  // 计算每节的宽度
    int step_y = h/layer;   //计算每层的高度

    painter.drawLine(50, 50, 50, h);  // 画竖线
    for (int x = 250; x < w; x += step_x)
    {
        painter.drawLine(x, 50, x, h);  // 画竖线
        x_position.push_back(x);
    }

    for (int y = 50; y < h; y += step_y)
    {
        painter.drawLine(50, y, w+250, y);  // 画横线
        y_position.push_back(y);
    }

    painter.drawLine(50, h, w+250, h);  // 画竖线
    painter.drawLine( w+250, 50,  w+250, h);  // 画竖线


    QPen pen(Qt::red);           // 定义一个红色画笔
    pen.setWidth(10);             // 设置画笔宽度
    painter.setPen(pen);
    for(int i=0;i<sub;i++)
    {
        QList<QPoint> pointlist_layer;
        for(int j=layer-1;j>=0;j--)
        {
            QPoint point;
            point.setX(x_position[i]);
            point.setY(y_position[j]);
            pointlist_layer.push_back(point);
            painter.drawPoint(point.x(), point.y());
        }
        mQRPoint.push_back(pointlist_layer);
    }
    ui->lb_image->setPixmap(pixmap);
}

void DeviceSettingsTab::loadTableSub()
{
    //锁定信号，防止自激循环
    ui->tb_sub->blockSignals(true);
    QList<QVariantList> allrecord;
    if(DeviceDBManager::getInstance()->mSubConfigDB.getAllRecord(allrecord))
    {
        //mylog<<allrecord;
        for(int i=0;i<allrecord.size();i++)
        {
            int row = ui->tb_sub->rowCount(); // get current number of rows
            ui->tb_sub->insertRow(row); // add a new empty row
            //添加表头
            QTableWidgetItem *headerItem = new QTableWidgetItem(QString("第%1节%2层 %3").arg(allrecord[i][1].toInt())
                    .arg(allrecord[i][2].toInt())
                    .arg(allrecord[i][3].toString()));
            ui->tb_sub->setVerticalHeaderItem(row, headerItem);
            //添加每一格单元格数据
            for (int j = 0; j < ui->tb_sub->columnCount(); ++j)
            {
                QTableWidgetItem *item = new QTableWidgetItem();
                item->setText(allrecord[i][j+4].toString());
                ui->tb_sub->setItem(row,j,item); // 获取单元格指针
            }
        }
    }
    ui->tb_sub->blockSignals(false);
    //可以连续选择多行
//    ui->tb_sub->setSelectionMode(QAbstractItemView::ExtendedSelection);
//    ui->tb_sub->setSelectionBehavior(QAbstractItemView::SelectRows);
}

void DeviceSettingsTab::loadTablePort()
{
    ui->tb_port->blockSignals(true);
    QList<QVariantList> allrecord;
    if(DeviceDBManager::getInstance()->mPortConfigDB.getAllRecord(allrecord))
    {
        for(int i=0;i<allrecord.size();i++)
        {
            int row = ui->tb_port->rowCount(); // get current number of rows
            ui->tb_port->insertRow(row); // add a new empty row
            //添加每一格单元格数据
            for (int j = 0; j < ui->tb_port->columnCount(); ++j)
            {
                QTableWidgetItem *item = new QTableWidgetItem();
                item->setText(allrecord[i][j+1].toString());
                ui->tb_port->setItem(row,j,item); // 获取单元格指针
            }
        }
    }
    ui->tb_port->blockSignals(false);
}

void DeviceSettingsTab::loadDeviceInfo()
{
    ui->ip_totalsub->setText(DeviceDBManager::getInstance()->getDeviceInfo("TotalSub").toString());
    ui->ip_totallayer->setText(DeviceDBManager::getInstance()->getDeviceInfo("TotalLayer").toString());
    ui->ip_portlayer->setText(DeviceDBManager::getInstance()->getDeviceInfo("PortLayer").toString());
    QString serverurl = DeviceDBManager::getInstance()->getDeviceInfo("ServerUrl").toString();
    ui->ip_serverip->setText(serverurl.split(":").at(0));
    ui->ip_serveriport->setText(serverurl.split(":").at(1));
    ui->ip_deviceip->setText(DeviceDBManager::getInstance()->getDeviceInfo("DeviceIP").toString());
    ui->ip_deviceport->setText(DeviceDBManager::getInstance()->getDeviceInfo("DevicePort").toString());
    ui->ip_devicegate->setText(DeviceDBManager::getInstance()->getDeviceInfo("DeviceGate").toString());
    ui->ip_deviceno->setText(DeviceDBManager::getInstance()->getDeviceInfo("DeviceNo").toString());
    ui->ip_devicename->setText(DeviceDBManager::getInstance()->getDeviceInfo("DeviceName").toString());
    ui->ip_maintitle->setText(DeviceDBManager::getInstance()->getDeviceInfo("MainTitle").toString());
}

void DeviceSettingsTab::loadTableUser()
{
    ui->tb_user->setModel(&model);
    ui->tb_user->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    ui->tb_user->horizontalHeader()->setStretchLastSection(true);
    model.setHorizontalHeaderLabels({"用户名","用户角色","创建时间","修改时间","用户状态"});
    ui->tb_user->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
    ui->tb_user->setSelectionMode(QAbstractItemView::SelectionMode::SingleSelection);

    QList<QVariantList> allrecord;
    if(DeviceDBManager::getInstance()->mUserInfoDB.getAllRecord(allrecord))
    {
        //添加数据
        freshTableUser(allrecord);
    }
}

void DeviceSettingsTab::freshTableUser(QList<QVariantList> allrecord)
{
    foreach(QVariantList record,allrecord)
    {
        QList<QStandardItem*> row;
        row.append(new QStandardItem(record[0].toString()));
        row.append(new QStandardItem(mRoleMap[record[4].toInt()]));
        row.append(new QStandardItem(record[5].toString()));
        row.append(new QStandardItem(record[6].toString()));
        row.append(new QStandardItem(mUserStateMap[record[7].toInt()]));
        // 在最后一行插入新数据
        model.insertRow(model.rowCount(), row);
    }
}

bool DeviceSettingsTab::stringToint(QString str, int &value, int base)
{
    bool ok ;
    value = str.toInt( &ok,base);
    return ok;
}

void DeviceSettingsTab::on_bt_startlocate_clicked()
{
    if(ui->ip_totallayer->text().isEmpty())
    {
        QMessageBox msgBox;
        msgBox.setText("请填写总层数！");
        msgBox.exec();
        return;
    }
    if(ui->ip_totalsub->text().isEmpty())
    {
        QMessageBox msgBox;
        msgBox.setText("请填写总节数！");
        msgBox.exec();
        return;
    }
    if(ui->ip_portlayer->text().isEmpty())
    {
        QMessageBox msgBox;
        msgBox.setText("请填写出货口所在层！");
        msgBox.exec();
        return;
    }

    QRLocateCommand cmd;
    if(ui->ck_cmdstep->checkState() == Qt::CheckState::Unchecked)
        cmd.isDebug = false;
    else
        cmd.isDebug = true;
    cmd.totalLayer = ui->ip_totallayer->text().toInt();
    cmd.totalSub = ui->ip_totalsub->text().toInt();
    cmd.portLayer = ui->ip_portlayer->text().toInt();
    cmd.subWidth.push_back(210);//210
    emit sig_sendQRLocateCmd(cmd);
}

void DeviceSettingsTab::on_btn_inoutcabnext_clicked()
{
    if(ui->ck_cmdstep->checkState() == Qt::CheckState::Checked)
    {
        QRLocateCommand cmd;

        cmd.isDebug = true;
        cmd.totalLayer = ui->ip_totallayer->text().toInt();
        cmd.totalSub = ui->ip_totalsub->text().toInt();
        cmd.portLayer = ui->ip_portlayer->text().toInt();

        if(cmd == lastcmd)
        {
            emit sig_sendQRLocateCmd(cmd);
        }
        else
        {
            QMessageBox msgBox;
            msgBox.setText("当前指令与上一条下发指令不同！");
            msgBox.exec();
        }
    }
    else
    {
        QMessageBox msgBox;
        msgBox.setText("请选择指令为调试状态！");
        msgBox.exec();
    }
}

void DeviceSettingsTab::on_btn_stop_clicked()
{
    emit sig_moveCmd("MOVESTOP",0,0,"");
    //emit sig_addLog("black",QString("发送全部停止指令"));
}

void DeviceSettingsTab::on_btn_subdelete_clicked()
{
    ui->tb_sub->clearContents();
}

void DeviceSettingsTab::on_btn_subedit_clicked()
{
    ui->tb_sub->setEditTriggers(QAbstractItemView::DoubleClicked);
}

void DeviceSettingsTab::on_btn_subsave_clicked()
{
    ui->tb_sub->setEditTriggers(QAbstractItemView::NoEditTriggers);
    QList<QVariantList> recordlist;
    mylog<<"row"<<ui->tb_sub->rowCount()<<" col:"<<ui->tb_sub->columnCount();
    for (int i = 0; i < ui->tb_sub->rowCount(); ++i)
    {
        QTableWidgetItem *headerItem =ui->tb_sub->verticalHeaderItem(i);
        QVariantList record;
        record.append(i);   //id
        record.append(headerItem->text().at(1).digitValue());   //第几节
        record.append(headerItem->text().at(3).digitValue());   //第几层
        record.append(headerItem->text().at(6));//AB面
        //最后一列位置不算
        for (int j = 0; j < ui->tb_sub->columnCount()-1; ++j)
        {
            QTableWidgetItem *item = ui->tb_sub->item(i, j); // 获取单元格指针
            if(item != nullptr)
            {
                QString text = item->text();
                // 处理单元格内容
                int value;
                if(stringToint(text,value,10))
                {
                    record.append(value);
                }
                else
                {
                    QMessageBox msgBox;
                    msgBox.setText(QString("保存失败！请检查第%1行%2列的内容是否是合法数字！").arg(i+1).arg(j+1));
                    msgBox.exec();
                    return;
                }
            }
            else
            {
                QMessageBox msgBox;
                msgBox.setText(QString("保存失败！第%1行%2列单元格未编辑！").arg(i+1).arg(j+1));
                msgBox.exec();
                return;
            }
        }
        record.append(0);
        record.append(0);
        mylog<<record;
        recordlist.push_back(record);
    }
    //在槽函数中写入数据库会导致程序崩溃，发送到调度核写数据库
    emit sig_subConfigInsertDB(recordlist);
}

void DeviceSettingsTab::on_btn_portsave_clicked()
{
    ui->tb_port->setEditTriggers(QAbstractItemView::NoEditTriggers);
    QList<QVariantList> recordlist;
    mylog<<"row"<<ui->tb_port->rowCount()<<" col:"<<ui->tb_port->columnCount();
    int portInc = 0;
    for (int i = 0; i < ui->tb_port->rowCount(); ++i)
    {
        QVariantList record;
        record.append(i);
        QTableWidgetItem *item = ui->tb_port->item(i, 0);

        if(item == nullptr)
        {
            QMessageBox msgBox;
            msgBox.setText(QString("保存失败！第%1行1列单元格未编辑！").arg(i+1));
            msgBox.exec();
            return;
        }

        int standard; // 规格
        if(stringToint(item->text(),standard,10))
        {
            portInc += standard*KincoServoManager::AxisX_1mm_INC;
            record.append(standard);
            record.append(portInc);
        }
        else
        {
            QMessageBox msgBox;
            msgBox.setText(QString("保存失败！请检查第%1行1列的内容是否是合法数字！").arg(i+1));
            msgBox.exec();
            return;
        }

        mylog<<record;
        recordlist.push_back(record);
    }
    //在槽函数中写入数据库会导致程序崩溃，发送到调度核写数据库
    emit sig_portConfigInsertDB(recordlist);
}

void DeviceSettingsTab::on_btn_portadd_clicked()
{
    int row = ui->tb_port->rowCount(); // get current number of rows
    ui->tb_port->insertRow(row); // add a new empty row
//    QTableWidgetItem *headerItem = new QTableWidgetItem(QString("规格%1").arg(row+1));
//    ui->tb_port->setVerticalHeaderItem(row, headerItem);
}

void DeviceSettingsTab::on_btn_portdelete_clicked()
{
    int row = ui->tb_port->rowCount(); // get current number of rows
    //默认的第一行规格1不能删除
    if(row == 0)
    {
        QMessageBox msgBox;
        msgBox.setText("列表已清空！");
        msgBox.exec();
        return;
    }

    //获取生成此行时的表头指针进行delete
    QTableWidgetItem *headerItem =ui->tb_port->verticalHeaderItem(row-1);
    if(headerItem != nullptr)
        delete headerItem;
    //删除行
    ui->tb_port->removeRow(row-1);
}

void DeviceSettingsTab::on_btn_portedit_clicked()
{
    ui->tb_port->setEditTriggers(QAbstractItemView::DoubleClicked);
}

void DeviceSettingsTab::on_btn_creatable_clicked()
{
    //先清空表格
    int row = ui->tb_sub->rowCount(); // get current number of rows
    ui->tb_sub->clearContents();
    while (ui->tb_sub->rowCount() > 0)
    {
        //获取生成此行时的表头指针进行delete
        QTableWidgetItem *headerItem =ui->tb_sub->verticalHeaderItem(ui->tb_sub->rowCount()-1);
        if(headerItem != nullptr)
            delete headerItem;
        //删除行
        ui->tb_sub->removeRow(ui->tb_sub->rowCount()-1);
    }


    int totalsub = ui->ip_totalsub->text().toInt()+1;
    int totallayer = ui->ip_totallayer->text().toInt();
    int portlayer = ui->ip_portlayer->text().toInt();
    for(int i=0;i<totalsub;i++)
    {
        for(int j=0;j<totallayer;j++)
        {
            //去掉出货口所在层
            if(i == 0 && j == portlayer-1)
                continue;
            row = ui->tb_sub->rowCount(); // get current number of rows
            ui->tb_sub->insertRow(row); // add a new empty row
            QTableWidgetItem *headerItem = new QTableWidgetItem(QString("第%1节%2层 A").arg(i).arg(j+1));
            ui->tb_sub->setVerticalHeaderItem(row, headerItem);
        }
        for(int j=0;j<totallayer;j++)
        {
            row = ui->tb_sub->rowCount(); // get current number of rows
            ui->tb_sub->insertRow(row); // add a new empty row
            QTableWidgetItem *headerItem = new QTableWidgetItem(QString("第%1节%2层 B").arg(i).arg(j+1));
            ui->tb_sub->setVerticalHeaderItem(row, headerItem);
        }
    }
}

void DeviceSettingsTab::slot_storgeUIRefreshTable()
{
    QList<QVariantList> allrecord;
    if(DeviceDBManager::getInstance()->mSubConfigDB.getAllRecord(allrecord))
    {
        for(int i=0;i<allrecord.size();i++)
        {
            QTableWidgetItem *item = ui->tb_sub->item(i,3);
            QString str = QString::number(allrecord[i][7].toInt())+","+QString::number(allrecord[i][8].toInt());
            item->setText(str);
        }
    }
}

void DeviceSettingsTab::on_btn_creatgoods_clicked()
{
    emit sig_goodsDistributeCalculate();
}
/***************************************************************
* 函数名称：on_tb_sub_cellChanged(int row, int column)
* 功能描述：修改任意一节的长度，其他相同节的长度自动修改
* 参数说明：
* 返回值说明：
***************************************************************/
void DeviceSettingsTab::on_tb_sub_cellChanged(int row, int column)
{
    ui->tb_sub->blockSignals(true);
    if(column != 0)
        return;
    QTableWidgetItem *item = ui->tb_sub->item(row, 0); // 获取单元格指针
    QString length = item->text();
    QTableWidgetItem *headerItem =ui->tb_sub->verticalHeaderItem(row);
    QVariantList record;

    QString sub = headerItem->text().at(1);   //第几节
    for(int i=0;i<ui->tb_sub->rowCount();i++)
    {
        if(i == row)
            continue;
        headerItem =ui->tb_sub->verticalHeaderItem(i);
        if(headerItem->text().at(1) == sub)
        {
            item = ui->tb_sub->item(i, 0); // 获取单元格指针
            if(item != nullptr)
            {
                item->setText(length);
            }
            else
            {
                QTableWidgetItem *item1 = new QTableWidgetItem();
                item1->setText(length);
                ui->tb_sub->setItem(i,0,item1); // 获取单元格指针
            }

        }
    }
    ui->tb_sub->blockSignals(false);
}



void DeviceSettingsTab::on_btn_saveconfig_clicked()
{
    QList<QVariantList> allrecords;
    allrecords.append({"TotalSub",ui->ip_totalsub->text()});
    allrecords.append({"TotalLayer",ui->ip_totallayer->text()});
    allrecords.append({"PortLayer",ui->ip_portlayer->text()});
    QString serverurl = ui->ip_serverip->text()+":"+ui->ip_serveriport->text();
    allrecords.append({"ServerUrl",serverurl});
    allrecords.append({"DeviceIP",ui->ip_deviceip->text()});
    allrecords.append({"DevicePort",ui->ip_deviceport->text()});
    allrecords.append({"DeviceGate",ui->ip_devicegate->text()});
    allrecords.append({"DeviceNo",ui->ip_deviceno->text()});
    allrecords.append({"DeviceName",ui->ip_devicename->text()});
    allrecords.append({"MainTitle",ui->ip_maintitle->text()});

    emit sig_deviceConfigDB(allrecords);
}

void DeviceSettingsTab::on_btn_useradd1_clicked()
{
    ui->fm_useradd->setVisible(true);
}

void DeviceSettingsTab::on_btn_pwdalter1_clicked()
{
    //如果有选中用户，则把该用户名直接填写到修改密码的用户名上
    QItemSelectionModel *pSelectedModel = ui->tb_user->selectionModel();
    if(pSelectedModel != nullptr && pSelectedModel->hasSelection())
    {
        //  获取该指令所在的行号
        int Index = pSelectedModel->selectedRows(0).at(0).row();
        ui->ip_pwdaltername->setText(model.item(Index,0)->data(Qt::DisplayRole).toString());
    }
    ui->fm_pwdalter->setVisible(true);
}

void DeviceSettingsTab::on_bnt_adduser1_clicked()
{
    if(DeviceDBManager::getInstance()->containsUserName(ui->ip_newname->text()))
    {
        QMessageBox msgBox;
        msgBox.setText("用户名已存在！");
        msgBox.exec();
        return;
    }

    if(ui->ip_newpwd1->text() != ui->ip_newpwd2->text())
    {
        QMessageBox msgBox;
        msgBox.setText("两次输入的密码不一致！");
        msgBox.exec();
        return;
    }
    QVariantList record{ui->ip_newname->text(),ui->ip_newpwd1->text(),-1,-1,ui->cb_role->currentIndex(),QDateTime::currentDateTime().toString(Qt::ISODate),QDateTime::currentDateTime().toString(Qt::ISODate),UserState::normal};
    emit sig_userInfoInsertDB(record);
    //清空填写框并将添加用户的frame隐藏
    ui->ip_newname->text().clear();
    ui->ip_newpwd1->text().clear();
    ui->ip_newpwd2->text().clear();
    ui->fm_useradd->setVisible(false);
}

void DeviceSettingsTab::slot_freshUserInfo(QList<QVariantList> allrecord)
{
    // 从末尾开始删除
    for(int i = (model.rowCount()-1); i>=0; i--)
    {
        model.removeRow(i);
    }
    //添加新数据
    freshTableUser(allrecord);
}

void DeviceSettingsTab::on_btn_userlock_clicked()
{
    //先判断有没有选择目标用户
    QItemSelectionModel *pSelectedModel = ui->tb_user->selectionModel();
    if(pSelectedModel == nullptr || !pSelectedModel->hasSelection())
    {
        QMessageBox msgBox;
        msgBox.setText("没有选择目标操作用户！");
        msgBox.exec();
        return;
    }

    //  获取该指令所在的行号
    int Index = pSelectedModel->selectedRows(0).at(0).row();
    QString rolename = model.item(Index,1)->data(Qt::DisplayRole).toString();
    int role = mRoleMap.key(rolename);
    int curuserrole;
    //查询当前登陆用户的权限
    if(DeviceDBManager::getInstance()->getUserRole(DeviceDBManager::getInstance()->CurLoginedUser,curuserrole))
    {
        //判断两者权限高低
        if(curuserrole>=role)
        {
            QMessageBox msgBox;
            msgBox.setText("当前登录用户权限等级低！");
            msgBox.exec();
            return;
        }

        QVariantMap condition;
        condition.insert("name",model.item(Index,0)->data(Qt::DisplayRole).toString());
        QVariantMap value;
        QString state = model.item(Index,4)->data(Qt::DisplayRole).toString();
        if(mUserStateMap.key(state) == UserState::normal)
        {
            value.insert("state",UserState::locked);
        }
        else if(mUserStateMap.key(state) == UserState::locked)
        {
            value.insert("state",UserState::normal);
        }

        //发送信号更新数据库
        emit sig_updateUserInfo(condition,value);
    }
}

void DeviceSettingsTab::on_btn_pwdalter2_clicked()
{
    //判断用户名是否已存在
    if(!DeviceDBManager::getInstance()->containsUserName(ui->ip_pwdaltername->text()))
    {
        QMessageBox msgBox;
        msgBox.setText("用户名不存在！");
        msgBox.exec();
        return;
    }

    //admin 超级管理员用户可以直接修改密码，不用输入原密码确认
    if(DeviceDBManager::getInstance()->CurLoginedUser != "admin")
    {
        //判断原密码
        QString name = ui->ip_pwdaltername->text();
        QString pwd;
        DeviceDBManager::getInstance()->getUserPwd(name,pwd);
        if(pwd != ui->ip_pwdalter1->text())
        {
            QMessageBox msgBox;
            msgBox.setText("原密码不正确！");
            msgBox.exec();
            return;
        }
    }

    if(ui->ip_pwdalter2->text() != ui->ip_pwdalter3->text())
    {
        QMessageBox msgBox;
        msgBox.setText("两次输入的密码不一致！");
        msgBox.exec();
        return;
    }

    //发送到调度核更新密码
    QVariantMap condition;
    condition.insert("name",ui->ip_pwdaltername->text());
    QVariantMap value;
    value.insert("pwd",ui->ip_pwdalter2->text());
    emit sig_updateUserInfo(condition,value);

    //清空填写框并将添加用户的frame隐藏
    ui->ip_pwdaltername->text().clear();
    ui->ip_pwdalter1->text().clear();
    ui->ip_pwdalter2->text().clear();
    ui->ip_pwdalter3->text().clear();
    ui->fm_pwdalter->setVisible(false);
}

void DeviceSettingsTab::on_btn_userdelete_clicked()
{
    //先判断有没有选择目标用户
    QItemSelectionModel *pSelectedModel = ui->tb_user->selectionModel();
    if(pSelectedModel == nullptr || !pSelectedModel->hasSelection())
    {
        QMessageBox msgBox;
        msgBox.setText("没有选择目标操作用户！");
        msgBox.exec();
        return;
    }

    //  获取该指令所在的行号
    int Index = pSelectedModel->selectedRows(0).at(0).row();
    QString rolename = model.item(Index,1)->data(Qt::DisplayRole).toString();
    int role = mRoleMap.key(rolename);
    int curuserrole;
    if(DeviceDBManager::getInstance()->getUserRole(DeviceDBManager::getInstance()->CurLoginedUser,curuserrole))
    {
        //判断权限
        if(curuserrole>=role)
        {
            QMessageBox msgBox;
            msgBox.setText("当前登录用户权限等级低！");
            msgBox.exec();
            return;
        }
        QVariantMap conditions{{"name",model.item(Index,0)->data(Qt::DisplayRole).toString()}};
        //发送到调度核删除
        emit sig_deleteUserInfo(conditions);
    }
}
